//三级分类相关配置仓库
import { defineStore } from "pinia";
import * as attrRequest from "@/api/product/attr";
import type { CategoryType } from "@/store/modules/types/type";
import type { CategoryResponseData } from "@/api/product/attr/type";

let useCategoryStore = defineStore("CategoryStore", {
  state: (): CategoryType => {
    return {
      chooseCategory: [], //已被选择的分类ID
      categoryList: [], //各个分类的数据
    };
  },
  actions: {
    async getAttrList(category: number = 1, fid: number = 0) {
      let result: CategoryResponseData = {
        code: 0,
        data: [],
        message: "",
        ok: false,
      };
      switch (category) {
        case 1:
          result = await attrRequest.reqgetCategory1();
          break;
        case 2:
          //当1级分类重新选择，需要清除掉2级和3级原来的数据，否则数据还是上次的
          this.chooseCategory.splice(1, 2);
          this.categoryList.splice(1, 2);
          result = await attrRequest.reqgetCategory2(fid);
          break;
        case 3:
          //当2级分类重新选择，需要清除掉3级原来的数据，否则数据还是上次的
          this.chooseCategory.splice(2, 1);
          this.categoryList.splice(2, 1);
          result = await attrRequest.reqgetCategory3(fid);
          break;
      }
      this.categoryList.push(result.data);
    },
  },
  getters: {},
});

export default useCategoryStore;
